package com.fingard.dsp.bank.directbank.ceb01;

import java.util.Date;
import java.util.List;

import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CEB01Deposit extends CEB01Base implements IDeposit {

    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception {
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            CEB01Header bankHeader = new CEB01Header();
            bankHeader.TransCode = "b2e001003";
            bankHeader.batchID = getBatchID();
            bankHeader.JnlDateTime = new Date();
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader_2(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("TransContent");
            xmlWriter.writeStartElement("ReqData");
            xmlWriter.writeElementString("AcNo", p_req7507.actNum);
            xmlWriter.writeElementString("ClientPatchID",bankHeader.batchID);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank_2Deposit(xmlWriter.toXmlString(), bankHeader.TransCode);//发送银行
            if(tmpStrRet[0].length() == 0){
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if(tmpBkReader != null){
                    while (tmpBkReader.read("/Transaction/TransContent")){
                        String tmpReturnCode = tmpBkReader.readedXElement.getSingleTextNotNull("ReturnCode");
                        String tmpReturnMsg = tmpBkReader.readedXElement.getSingleTextNotNull("ReturnMsg");
                        if ("0000".equals(tmpReturnCode)) {
                            List<XElement> readXElementList=tmpBkReader.readedXElement.getChildList("RespData");
                            for(XElement readXElement:readXElementList){
                                RetAts7507Detail retAts7507Detail = new RetAts7507Detail();
                                retAts7507Detail.actNum=p_req7507.actNum;//银行账号
                                retAts7507Detail.actArea=p_req7507.actArea;//账号地区区域码
                                retAts7507Detail.depositCode=readXElement.getSingleTextNotNull("ZHANGH").trim();//定存单号
                                retAts7507Detail.startDate=readXElement.getSingleTextNotNull("QIXIRQ").trim();//起始日期
                                retAts7507Detail.endDate=readXElement.getSingleTextNotNull("DAOQRQ").trim();//截止日期
                                retAts7507Detail.valueDate=readXElement.getSingleTextNotNull("QIXIRQ").trim();//起息日期
                                String terms=readXElement.getSingleTextNotNull("CUNQII").trim();//期限
                                retAts7507Detail.terms=getBankConfig().getMapValueByKey("termMapping",terms);//存款期限 (七天`1,一个月`2,三个月`3,六个月`4,一年`5,二年`6,三年`7,五年`8,其它`9)
                                if(StringHelper.isNullOrEmpty(retAts7507Detail.terms)){
                                    retAts7507Detail.terms="9";//其它
                                }
                                retAts7507Detail.currencyID=convertCurToATS(readXElement.getSingleTextNotNull("HUOBDH"));//币种
                                retAts7507Detail.noteMoney=readXElement.getSingleTextNotNull("LJZQJE").trim();//金额
                                retAts7507Detail.rate=readXElement.getSingleTextNotNull("SHIJLL").trim();//利率(%)
                                retAts7507Detail.interestMoney=readXElement.getSingleTextNotNull("LEIJLX").trim();//利息
                                retAts7507Detail.depositStates="";//存单状态 (正常`1,冻结`2,未启用`3,已退票`4,销户`5,挂失`7,损益`6,作废`9)
                                retAts7507Detail.dumpFlag="";//到期处理方式 (不转存`1,本息转存`2,本金转存，利息转活期`3)
                                retAts7507Detail.drawDate="";//上划日期
                                retAts.writeRetDetail(retAts7507Detail);
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpReturnCode +" : "+tmpReturnMsg;
                        }
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("查无存单");
        }
        retAts.writeEnd();
        return retAts;
    }
}
